package com.email;

import org.springframework.mail.*;
import org.springframework.mail.javamail.*;

import javax.mail.*;
import java.text.*;
import java.util.*;

/**
 * @author 代廷波
 * @className: Test
 * @description:
 * @create 2018/11/27 0027-17:21
 **/
public class Test {
    public static void simpleMailSend() {
        //获取邮件发送器
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding("UTF-8");
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setProtocol("smtp");
        javaMailSender.setPort(587);
        javaMailSender.setUsername("190528162@qq.com");
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        props.setProperty("mail.smtp.auth", "true");
        // 在第三方登录QQ邮箱
        Session session = Session.getDefaultInstance(props,new Authenticator(){
            @Override
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("190528162@qq.com","prjtzcerbuahbgjh");
            }
        });
        javaMailSender.setSession(session);
        // 设置邮件信息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        // 发件地址
        mainMessage.setFrom("190528162@qq.com");
        // 收件地址
        mainMessage.setTo("cqdtb@163.com");
        Date date = new Date();
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String strDate = format1.format(date);
        // 主题
        mainMessage.setSubject(strDate+"--java邮箱发送主题");
        // 内容
        mainMessage.setText("具体内容请看详情.............");
        javaMailSender.send(mainMessage);
    }

    public static void main(String[] args) {
        String email="190528162@qq.com";
        String subject="subjectsubjectsubjectsubjectsubjectsubject";
        String msg="prjtzcerbuahbgjh";
        simpleMailSend();
    }
}
 